﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Infrastructure.ObjectModel;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;

namespace B10_Ex02_Elad_034695114_Meyron_026607788
{
    /// <summary>
    /// Enemy sprite Class
    /// </summary>
    public class EnemySprite : EnemySpriteBase
    {
        #region Data Members

        private const int k_FireFrequency = 5000;
        private static Random s_rnd = new Random();
        private List<EnemyBullet> m_Bullets;

        #endregion

        public List<EnemyBullet> Bullets
        {
            get
            {
                return m_Bullets;
            }

            protected set
            {
                m_Bullets = value;
            }
        }

        #region Ctor

        /// <summary>
        /// Constructs an enemy sprite
        /// </summary>
        /// <param name="i_game">The game</param>
        /// <param name="i_assetName">The texture</param>
        /// <param name="i_tint">The color of the enemy</param>
        public EnemySprite(Game i_game, string i_assetName, Color i_tint, int i_points)
            : base(i_assetName, i_game)
        {
            this.TintColor = i_tint;
            this.Points = i_points;
            this.Bullets = new List<EnemyBullet>();
        }

        #endregion

        #region Other Methods

        public override void Update(GameTime gameTime)
        {
            // Randomly decide firing
            handleBullets();

            base.Update(gameTime);
        }

        /// <summary>
        /// Fires a bullet to the top
        /// </summary>
        protected void handleBullets()
        {
            // Check for passed Bullets and removes them from the collection
            for (int nBulletIndex = 0; nBulletIndex < this.Bullets.Count; nBulletIndex++)
            {
                if (!this.Bullets[nBulletIndex].Enabled)
                {
                    this.Bullets.RemoveAt(nBulletIndex);
                    nBulletIndex--;
                }
            }

            // Randomly decide firing
            if (s_rnd.Next(k_FireFrequency) == 0)
            {
                EnemyBullet bltNew = new EnemyBullet(
                     this.Game,
                     new Vector2(Position.X + (m_Width / 2), Position.Y + (this.Height / 2)));
                m_Bullets.Add(bltNew);
            }
        } 

        #endregion
    }
}